package Classes.GameUtils
{
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	import flash.system.Capabilities;

	public class GU_CopyDisplay
	{
		public function GU_CopyDisplay()
		{
		}
		
			/**
			 * 图片克隆 
			 * @param target
			 * @param item
			 * @param autoAdd
			 * @return 
			 */		
			public static function duplicateDisplayObject(target:DisplayObject,item:*, autoAdd:Boolean = false):DisplayObject {
				var targetClass:Class = Object(target).constructor;
				
				var duplicate:DisplayObject	= new targetClass(item) as DisplayObject;
				// duplicate properties
				duplicate.transform = target.transform;
				duplicate.filters = target.filters;
				duplicate.cacheAsBitmap = target.cacheAsBitmap;
				duplicate.opaqueBackground = target.opaqueBackground;
				if (target.scale9Grid) {
					var rect:Rectangle = target.scale9Grid;
					if (Capabilities.version.split(" ")[1] == "9,0,16,0"){
						// Flash 9 bug where returned scale9Grid as twips
						rect.x /= 20, rect.y /= 20, rect.width /= 20, rect.height /= 20;
					}
					duplicate.scale9Grid = rect;
				}
				// Flash 10 only
				// duplicate.graphics.copyFrom(target.graphics);
				// add to target parent's display list
				// if autoAdd was provided as true
				if (autoAdd && target.parent) {
					target.parent.addChild(duplicate);
				}
				return duplicate;
			}
	}
}